﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Dukous.Web;
using Common.Logging;
using Spring.Context;
using Spring.Context.Support;
using Dukous.Web.Mvc;

namespace Celine.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static bool __initialized = false;

        private static object __syncObject = new object();

        private static readonly ILog logger = LogManager.GetLogger(typeof(MvcApplication));

        public IList<IGlobalModule> GlobalModules { get; set; }

        public IList<ICustomModelBinder> ModelBinders { get; set; }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

        public override void Init()
        {
            if (!__initialized)
            {
                lock (__syncObject)
                {
                    if (!__initialized)
                    {
                        IApplicationContext appContext = ContextRegistry.GetContext();

                        appContext.ConfigureObject(this, "GlobalModuleInitializer");
                        appContext.ConfigureObject(this, "ModelBinderInitializer");

                        if (this.GlobalModules != null)
                        {
                            foreach (IGlobalModule module in this.GlobalModules)
                            {
                                logger.Info("初始化 " + module.Name);
                                try
                                {
                                    module.Initialize(this, appContext);
                                }
                                catch (Exception ex)
                                {
                                    logger.Warn(module.Name + "初始化异常", ex);
                                }
                            }
                        }

                        if (this.ModelBinders != null)
                        {
                            foreach (ICustomModelBinder modelBinder in this.ModelBinders)
                            {
                                foreach (Type type in modelBinder.SupportedTypes)
                                {
                                    logger.Info(string.Concat("注册模型绑定器：", type.Name, " - ", modelBinder.GetType().Name));
                                    System.Web.Mvc.ModelBinders.Binders.Add(type, modelBinder);
                                }
                            }
                        }
                    }
                }
            }

            __initialized = true;

            base.Init();
        }
    }
}